package com.qingfeng.provide.controller;

import com.qingfeng.core.data.ResultData;
import com.qingfeng.provide.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:LWY
 * @date: 2017/11/15 16:05
 * @modified:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    /**
     * 发送验证码
     *
     * @param phone 手机号码
     * @param type  类型(1：登录、2：找回密码、3：绑定手机等)
     */
    @RequestMapping("/auth/{phone}/{type}")
    public ResultData auth(@PathVariable String phone, @PathVariable String type) {
        String code = userService.auth(phone, type);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "发送成功,3分钟后失效。", code);
        return resultData;
    }

    /**
     * 登录
     *
     * @param request
     */
    @RequestMapping("/login")
    public ResultData login(HttpServletRequest request) {
        String phone = request.getParameter("phone");//手机号码
        String auth = request.getParameter("auth");//授权密钥
        String type = request.getParameter("type");// 登录类型(1:验证码，2密码)
        Map<String, Object> map = userService.login(phone, auth, type);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", map);
        return resultData;
    }

    /**
     * 找回密码
     *
     * @param request
     */
    @RequestMapping("/forget")
    public ResultData forget(HttpServletRequest request) {
        String phone = request.getParameter("phone");//手机号码
        String auth = request.getParameter("auth");//验证码
        String pwd = request.getParameter("pwd");//新密码
        userService.forget(phone, auth, pwd);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "密码修改成功", "");
        return resultData;
    }

    /**
     * 绑定手机
     *
     * @param request
     */
    @RequestMapping("/bindPhone")
    public ResultData bindPhone(HttpServletRequest request) {
        String phone = request.getParameter("phone");//手机号码
        String auth = request.getParameter("auth");//验证码
        String id = request.getParameter("id");//用户id
        userService.bindPhone(phone, auth, id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "绑定成功，以后用此号码登录", "");
        return resultData;
    }


    /**
     * 获取用户信息
     *
     * @param
     */
    @RequestMapping("/userinfo")
    public ResultData userinfo(HttpServletRequest request) {
        String id = request.getParameter("id");
        Map<String, Object> map = userService.userinfo(id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "用户信息", map);
        return resultData;
    }

    /**
     * 保存用户信息
     *
     * @param request
     */
    @RequestMapping("/userinfo/save")
    public ResultData saveInfo(HttpServletRequest request) {
        String id = request.getParameter("id");//id
        String nick_name = request.getParameter("nick_name");//昵称
        String sex = request.getParameter("sex");//性别
        String grade_id = request.getParameter("grade_id");//年级
        String major = request.getParameter("major");//专业
        String specialty = request.getParameter("specialty");//特长，逗号隔开
        userService.saveInfo(id, nick_name, sex, grade_id, major, specialty);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "资料修改成功", "");
        return resultData;
    }

    /**
     * 上传学生证件、身份证件
     * type：{1：学生证件、2：身份证件}
     */
    @RequestMapping("/credential/upload/{type}")
    public ResultData credential(HttpServletRequest request, @PathVariable Integer type) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        //用户id
        String user_id = request.getParameter("id");
        userService.saveCredential(user_id, type, files);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "上传证件成功", "");
        return resultData;

    }
}
